package flash.cards;

import java.io.File;
import java.io.FilenameFilter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;


public class Course extends ListActivity {

	private static final int ABOUT=1;
	private static final int SETTING=2;
	private String[] mFile = new String[]{};
	private String FilePath="/sdcard/data/flash_cards/";

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, ABOUT, 2, R.string.about).setIcon(R.drawable.ic_menu_info_details);
		menu.add(0, SETTING, 3, R.string.set).setIcon(R.drawable.ic_menu_preferences);
		return super.onCreateOptionsMenu(menu);
	}


	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getApplicationContext();

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				Intent intent = new Intent();
				intent.putExtra("filename", ((TextView) view).getText()+".vocal");
				intent.putExtra("FilePath", FilePath);
				intent.setClass(Course.this, setting.class);
				Course.this.startActivity(intent);
			}
		});

		String[] mFile=getFileList(FilePath);
		setListAdapter(new ArrayAdapter<String>(this,R.layout.course,mFile));
	}



	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==ABOUT){
			new AlertDialog.Builder(Course.this)
            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(R.string.about)
            .setMessage(R.string.imformation)
            .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })
            .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })
            .create().show();
		}
		else if(item.getItemId() == SETTING){
			Intent intent = new Intent();
			intent.setClass(Course.this, Preferences.class);
			Course.this.startActivity(intent);
		}
		return super.onOptionsItemSelected(item);
	}

	private String[] getFileList(String path){
		FilenameFilter fnf= new FilenameFilter(){
			public boolean accept(File dir,String filename){
				if(filename.endsWith(".vocal"))return true;
				return false;
			}
		};
		File flist = new File(path);
		mFile=flist.list(fnf);
		for(int i=0;i <mFile.length;i++){
			mFile[i]=mFile[i].substring(0,mFile[i].length()-6);
		}
		return mFile;
	}


}